﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RailwayReservationWeb
{
    public partial class my_profile : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;
        private Account authinfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            var authsvc = new AuthService();
            try
            {
                if (Session["authinfo"] == null)
                {
                    Response.Redirect("~/login.aspx");
                }
                else
                {
                    authinfo = (Account)Session["authinfo"];
                }
            }
            catch (Exception) { }
            db = new RailwayReservationDataContext();
            if (!IsPostBack)
            {
                int id = authinfo.AccountId;
                var ob = db.Accounts.First(p => p.AccountId.Equals(id));
                if (ob != null)
                {
                    txtUserName.Text = ob.UserName;
                    txtEmail.Text = ob.Email;
                    txtFullName.Text = ob.FullName;
                    txtSSN.Text = ob.SSN;
                    txtDob.Text = ob.DoB.ToString("MM/dd/yyyy");
                    txtGender.Text = GetGenderText(ob.Gender);
                    txtAddress.Text = ob.Address;
                    txtPhone.Text = ob.Phone;

                    const string gravatarRoot = "http://www.gravatar.com/";
                    const string gravatarAvatar = "http://www.gravatar.com/avatar/";
                    string emailHash = Checksum.MD5(authinfo.Email.Trim());
                    imgGravatarBig.ImageUrl = gravatarAvatar + emailHash + "?s=160";
                    btnGravatar.PostBackUrl = gravatarRoot + emailHash;
                }
            }
        }

        protected string GetGenderText(short gender)
        {
            switch (gender)
            {
                case 0: return "Female";
                case 1: return "Male";
            }
            return String.Empty;
        }
    }
}